class List{
    constructor(){
        this.url = "http://localhost:3000/api";
        this.cont = document.querySelector(".jsxr");
        this.load();
        this.addEvent();
    }
    addEvent(){
        const that = this;
        this.cont.addEventListener('click',function(eve){
            const e = eve || window.event;
            const target = e.target || e.srcElement;
            if(target.className === "add"){
                that.id = target.parentNode.getAttribute("index")
                that.setLocal();
            }
        })
    }
    setLocal(){
        const goods = localStorage.getItem("goods") ? JSON.parse(localStorage.getItem("goods")) : [];
        if(goods.length < 1){
            goods.push({
                id:this.id,
                num:1,
                flag:0
            })
        }else{
            let i = 0;
            const flag = goods.some((val,idx)=>{
                i = idx;
                return val.id === this.id
            })
            if(flag){
                goods[i].num++;
            }else{
                goods.push({
                    id:this.id,
                    num:1,
                    flag:0
                })
            }
        }

        localStorage.setItem("goods", JSON.stringify(goods))
    }
    load(){
        ajax({
            url:this.url,
            data:{
                type:"getGoods"
            },
            success:res=>{
                this.res = JSON.parse(res).msg;
                this.render();
            }
        })
    }
    render(){
        let str = "";
        this.res.forEach(val=>{
            str += `
            
            <li class="b" index="${val.proId}">
                        <img src="${val.img}" alt="">
                        <div class="b-a">${val.proName}</div>
                        <div class="b-b">
                        <p >【11月10日20:00开售, 预售享: ①整点升级vivo背包，价同11号; ②赠品可选半年延保; ③3期免息】</p> 
                        <span></span>
                        </div>
                        <div class="b-d">￥${val.price}</div>
                        <input type="button" class="add" value="加入购物车">
                    </li></a>
                    `
        })
        this.cont.innerHTML = str;
    }
    }
        new List();












   